/**
 * Copyright (C) BFH www.bfh.ch 2011
 * Code written by: Patrick Dobler, Marc Folly
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.ti.kybernetik;

import java.awt.Dimension;

import javax.swing.SwingUtilities;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.SlickException;

import ch.bfh.ti.kybernetik.engine.controller.Simulator;
import ch.bfh.ti.kybernetik.engine.controller.SimulatorFactory;
import ch.bfh.ti.kybernetik.engine.controller.lightBulb.LightBulbController;
import ch.bfh.ti.kybernetik.engine.controller.lightBulb.LightBulbControllerFactory;
import ch.bfh.ti.kybernetik.engine.controller.roboter.RoboterController;
import ch.bfh.ti.kybernetik.engine.controller.roboter.RoboterControllerFactory;
import ch.bfh.ti.kybernetik.gui.slick.GuiSimulator;
import ch.bfh.ti.kybernetik.gui.swing.SimulatorControlGuiFrame;

/**
 * The Main Class of the Simulator
 * 
 */
public class Main {
	private final static int INIT_LIGHTBULBCONTROLLERS_NUMBERS = 2;

	private static final int INIT_ROBOTERCONTROLLER_NUMBERS = 1;

	/**
	 * @param args
	 */
	public static void main(String[] args) throws SlickException {

		// 70% of Screen Size is reserved for the Simulation GUI
		Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		final int width = (int) (screensize.getWidth() * 0.8f);
		final int height = (int) (screensize.getHeight() * 0.8f);

		final RoboterController[] roboterControllers = buildRobotorControllers(width, height);
		final LightBulbController[] lightBulbControllers = buildLightBulbControllers(width, height);
		final Simulator simulator = buildSimulator(roboterControllers, lightBulbControllers);

		// Start Simulator Thread
		Thread t = new Thread(simulator);
		t.setName("simulatorThread");
		t.start();

		// Initialize Slick GUI
		final GuiSimulator game = new GuiSimulator(simulator);
		Thread t2 = new Thread(new Runnable() {
			@Override
			public void run() {

				try {

					AppGameContainer app = new AppGameContainer(game);
					app.setDisplayMode(width, height, false);
					// app.setFullscreen(true);
					app.start();
				} catch (SlickException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		t2.setName("slickThread");
		t2.start();

		// Start Swing GUI Controller Thread
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				SimulatorControlGuiFrame ex = new SimulatorControlGuiFrame(simulator, game);
				ex.setVisible(true);
			}
		});

	}

	private static LightBulbController[] buildLightBulbControllers(int width, int height) {

		final LightBulbController[] lightBulbControllers = new LightBulbController[INIT_LIGHTBULBCONTROLLERS_NUMBERS];
		for (int i = 0; i < lightBulbControllers.length; i++) {
			LightBulbController lbc2 = LightBulbControllerFactory.createRandomLightBulbConroller(width, height);
			lightBulbControllers[i] = lbc2;
		}

		return lightBulbControllers;
	}

	private static RoboterController[] buildRobotorControllers(int width, int height) {
		RoboterController[] roboterControllers = new RoboterController[INIT_ROBOTERCONTROLLER_NUMBERS];
		for (int i = 0; i < roboterControllers.length; i++) {
			roboterControllers[i] = RoboterControllerFactory.createRandomRoboterController(width, height);
		}
		return roboterControllers;
	}

	private static Simulator buildSimulator(RoboterController[] roboterControllers, final LightBulbController[] lightBulbControllers) {
		return SimulatorFactory.generateDefaultSimulator(roboterControllers, lightBulbControllers);
	}
}
